<template>
    <el-table :data="state.data" height="900" 
    :default-sort="state.sort"
    @sort-change="sortChange"
    @cell-click="cellClick"
    :cell-class-name="tableCellClassName"
    style="width: 100%">
        <el-table-column prop="date" label="date"  width="210" sortable/>
        <el-table-column prop="stock" label="股票" width="200">
            <template #default="scope">
                <ToStockDetail :stock="scope.row.stock" />
            </template> 
        </el-table-column>
        <el-table-column prop="industry" label="industry"  width="160" sortable/>
        <el-table-column prop="weekPct" label="week_pct"  width="120" sortable/>
        <el-table-column prop="disPct" label="disPct"  width="120" sortable/>
        <el-table-column prop="lp" label="lp"  width="120" sortable/>
        <el-table-column prop="curDisDuration" label="距今周数" width="120" sortable/>
        <el-table-column prop="duration" label="持续周数" width="120" sortable/>
        <el-table-column prop="curPct" label="curPct" width="120" sortable/>
        <el-table-column prop="cur30wPct" label="cur30wPct" width="120" sortable/>
        <el-table-column prop="cur60wPct" label="cur60wPct" width="120" sortable/>
        <el-table-column prop="incNum" label="incNum" width="120" sortable/>
        <el-table-column prop="pctSum" label="pctSum" width="120" sortable/>
    </el-table>
</template>

<script>
// 个股周数据详情页
import { reactive, watch } from 'vue';
import ToStockDetail from '../ToStockDetail.vue';
import { getGB30WLowData, getGB30WTopData, getGB60WTopData } from '../../service/gbSta';
import { getLpClassName } from '../../utils/help';

export default {
    name: 'WeekStockDetail',
    components: {
        ToStockDetail,
    },

    setup() {

        const baseKey = "MonitorPrice"
        const selectKey = baseKey + "Select"
        const sortPropKey = baseKey + "SortProp"
        const sortOrderKey = baseKey + "SortOrder"

        const state = reactive({
            rawData: [],
            data: [],
            operationSelected: "",
            sort: { 
                prop: sessionStorage.getItem(sortPropKey) || 'date' , 
                order: sessionStorage.getItem(sortOrderKey) || 'descending' 
            }
        })

        getGB30WLowData().then(res => {
            state.rawData = res.data
            state.data = res.data
        })

        const sortChange = (column, prop, order) => {
            sessionStorage.setItem(sortPropKey, column.prop)
            sessionStorage.setItem(sortOrderKey, column.order)
        }

        const tableCellClassName = ({row, column, rowIndex, columnIndex}) => {
            let columnProperty = column.property
            let val =  row[columnProperty]
            if (columnProperty == "lp") {
                return getLpClassName(val)
            }
            if (["curPct","cur60wPct","cur30wPct"].includes(columnProperty)) {
                if (val < 0) {
                    return "lt0-cell"
                }
            }
            return ''
        }

        const cellClick = (row, column, cell, event) => {
            var columnProperty = column.property

            if (['industry','date','minDate','maxDate','curPct','cur60wPct','nextWeekPct'].includes(columnProperty)) {
                let val = row[columnProperty]
                if (state.operationSelected != "" && state.operationSelected == val) {
                    state.operationSelected = ""
                    state.data = state.rawData
                } else {
                    state.operationSelected = val
                    if (["curPct","cur60wPct","nextWeekPct"].includes(columnProperty)) {
                        if (val >= 0) {
                            state.data = state.rawData.filter(item => item[columnProperty] >= 0)
                        } else {
                            state.data = state.rawData.filter(item => item[columnProperty] < 0)
                        }
                    } else {
                        state.data = state.rawData.filter(item => item[columnProperty] == val)
                    }
                }
            }
        }

        return {state, tableCellClassName, sortChange, cellClick}
    }
}
</script>